package glossos.parsing.fsm;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import glossos.parsing.CharMatcher;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

/**
 *
 * @author Otavio Macedo
 * @since <pre>10/01/2009</pre>
 */
@RunWith(MockitoJUnitRunner.class)
public class StateTestCase {

    State state, returnState;
    @Mock CharMatcher matcher1, matcher2;
    @Mock Transition transition1, transition2;

    @Before
    public void setUp() throws Exception {
        state = new State("start");
        returnState = new State("return");
        matcher1 = mock(CharMatcher.class);
        matcher2 = mock(CharMatcher.class);
        transition1 = mock(Transition.class);
        transition2 = mock(Transition.class);
    }

    @Test
    public void stringRepresentation() {
        assertThat(state.toString(), is("start"));
    }

    @Test
    public void triggerRightTransition() {
        when(transition1.isDefinedFor('x')).thenReturn(false);
        when(transition2.isDefinedFor('x')).thenReturn(true);
        when(transition2.trigger('x')).thenReturn(returnState);

        state.addTransition(transition2);
        state.addTransition(transition1);
        
        assertThat(state.next('x'), is(returnState));
    }

    @Test
    public void stateReturnsItselfWhenCharacterDoesNotMatchesAnyTransition() {
        when(transition1.isDefinedFor('x')).thenReturn(false);
        when(transition2.isDefinedFor('x')).thenReturn(false);

        state.addTransition(transition2);
        state.addTransition(transition1);

        assertThat(state.next('x'), is(state));
    }
}
